﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.ServiceModel.DomainServices.Server;
using System.Web;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Extensions;
using NetBay.Core.Log;
using NetBay.Core.Mail.SMTP.Assync;
using Rio.Flow.Business.Repository;
using Rio.Flow.DataAccess;
using Rio.Framework.Business.Repository;
using Rio.Framework.RioManager;
using System.Configuration;
using Rio.Doc.Business.Repository;
using Rio.Applications.Web.RioWCFWorkflow;
#endregion

namespace Rio.Applications.Web.Services
{
    /// <summary>
    /// Class to handle Business entities in a RIA services
    /// </summary>
    public partial class BusinessEntitiesDomainService : DomainService
    {
        [Query]
        public IEnumerable<ReturnLong> CountMessageFlowSentByMe()
        {
            var l_oRet = new List<ReturnLong>();
            try
            {
                User currentUser = HttpContext.Current.Session[ParameterSession.CurrentUser.szGetDescription()] as User;
                if (currentUser == null)
                    throw new Exception("The current user was not found in session!");

                l_oRet.Add(new ReturnLong(MessageFlowRepository.CountMessageFlowSentByMe(currentUser.UserId)));
            }
            catch (Exception ex)
            {
                //LogManager.Instance().LogError("Exception in CountMessageFlowSentByMe : " + ex.Message);
            }
            return l_oRet;
        }

        [Query]
        public IEnumerable<ReturnLong> CountMessageFlowInMyBox()
        {
            var l_oRet = new List<ReturnLong>();
            try
            {
                User currentUser = HttpContext.Current.Session[ParameterSession.CurrentUser.szGetDescription()] as User;
                if (currentUser == null)
                    throw new Exception("The current user was not found in session!");

                l_oRet.Add(new ReturnLong(MessageFlowRepository.CountMessageFlowInMyBox(currentUser.UserId, 0)));
            }
            catch (Exception ex)
            {
                //LogManager.Instance().LogError("Exception in CountMessageFlowInMyBox : " + ex.Message);
            }
            return l_oRet;
        }

        [Query]
        public IEnumerable<MessageFlow> GetMessageFlowSentByMe()
        {

            User l_oCurrentUser  = HttpContext.Current.Session[ParameterSession.CurrentUser.szGetDescription()] as User;

            List<MessageFlow> l_oRet = new List<MessageFlow>();

            try 
            {
                if (l_oCurrentUser == null)
                    throw new Exception("Current user is not valid!!!");
                l_oRet = MessageFlowRepository.GetMessageFlowSentByMe(l_oCurrentUser.UserId);
            }
            catch (Exception ex)
            {
                MessageFlow l_oMessageFlow = new MessageFlow("", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oMessageFlow.FullErrorMessage);
                l_oRet.Add(l_oMessageFlow);
            }
            return l_oRet;
        }

        /// <summary>
        /// Retreive the inbox message
        /// </summary>
        /// <returns></returns>
        [Query]
        public IEnumerable<MessageFlow> GetMessageFlowInMyBox()
        {

            User l_oCurrentUser = HttpContext.Current.Session[ParameterSession.CurrentUser.szGetDescription()] as User;

            List<MessageFlow> l_oRet = new List<MessageFlow>();

            try
            {
                if (l_oCurrentUser == null)
                    throw new Exception("Current user is not valid!!!");
                l_oRet = MessageFlowRepository.GetMessageFlowInMyBox(l_oCurrentUser.UserId);


            }
            catch (Exception ex)
            {
                MessageFlow l_oMessageFlow = new MessageFlow("", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oMessageFlow.FullErrorMessage);
                l_oRet.Add(l_oMessageFlow);
            }
            return l_oRet;
        }

        /// <summary>
        /// Retreive all associations of TdId and its type of Flow (In/Out)
        /// </summary>
        /// <returns>All associations</returns>
        [Query]
        public IEnumerable<ParamMessageFlow> GetAllParamMessageFlow()
        {
            List<ParamMessageFlow> l_oRet = new List<ParamMessageFlow>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("");
                //l_oRet = FlowManager.ParamMessageFlows;
                l_oRet = MessageFlowRepository.Load(); 
            }
            catch (Exception ex)
            {
                ParamMessageFlow l_oReturnValue = new ParamMessageFlow("", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnValue.FullErrorMessage);
                l_oRet.Add(l_oReturnValue);
            }
            return l_oRet;
        }

        /// <summary>
        /// Add a mssage Fow
        /// </summary>
        /// <param name="p_lEmetteurId">Identifier of the source user</param>
        /// <param name="p_lChronoId">Identifier of the chrono </param>
        /// <param name="p_lDocId">Document Identifier</param>
        /// <param name="p_sObjet">Object</param>
        /// <param name="p_sMemo">Text</param>
        /// <param name="p_sGroupDestinataire">All destination group seperate with a ";"</param>
        /// <returns>The identifier of the message flow</returns>
        [Query(HasSideEffects = true)]
        public IEnumerable<ReturnLong> AddMessageFlow(long p_lEmetteurId, long p_lChronoId, List<long> p_oDocIdList, string p_sObjet, string p_sMemo, string p_sGroupDestinataire, Boolean p_bSendAlertByMail, Boolean p_bNotifyReceivers, int p_iTypeFlow = 0, string p_sChronoWording = null, string p_sFolderURL = null)
        {
            List<ReturnLong> l_oRet = new List<ReturnLong>();
            ReturnLong l_oReturnLong = new ReturnLong();
            ReturnBoolean l_oReturnBoolean;
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lEmetteurId<" + p_lEmetteurId + "> p_lChronoId<" + p_lChronoId + "> p_lDocId<" + p_oDocIdList.ToString() + "> p_sObjet<" + p_sObjet + "> p_sMemo<" + p_sMemo + "> p_sGroupDestinataire<" + p_sGroupDestinataire.ToString(";") + ">)...");
                User l_oUser = GetUser();

                int cpt = 0;
                long l_lMessageFlowId = 0;
                bool l_bResult = true;
                long l_lChonoId = p_lChronoId;
                foreach (long l_lDocId in p_oDocIdList)
                {

                    // Retreive info from document
                    Document l_oDocument = Doc.Business.Repository.DocumentRepository.GetDocumentById(l_lDocId);
                    // Add a historic of user action
                    AddActionHistoric((int)Constant.UserActions.VENTILATION, 0, 0, l_lDocId.ToString(), l_oDocument.OriginalNameFile);

                    if (cpt == 0)
                    {

                        //if (p_iTypeFlow != 0)
                        //{
                        //    Tuple<long, string> l_oReturnTuple = MessageFlowRepository.GenerateChrono(p_iTypeFlow, SettingsManager.cStoreSettings.Parameter_ChronoPrefix, 0);
                        //    l_lChonoId = l_oReturnTuple.Item1;
                        //}

                        l_oReturnLong = MessageFlowRepository.AddMessageFlow(p_lEmetteurId, l_lChonoId, l_lDocId, p_sObjet, p_sMemo, p_sGroupDestinataire, p_bSendAlertByMail, p_sFolderURL);
                        if (l_oReturnLong != null && l_oReturnLong.IsValid)
                        {
                            l_lMessageFlowId = l_oReturnLong.Value;
                        }
                        else
                        {
                            l_bResult = false;
                            break;
                        }
                    }
                    else
                    {
                        l_oReturnBoolean = MessageFlowRepository.AddDocumentToMessageFlow(l_lMessageFlowId, l_lDocId);

                    }
                    cpt++;
                }


                // Send notification mail if all ok
                if (l_bResult && p_bNotifyReceivers)
                {

                    List<MailAddress> m_oMailAddressList = new List<MailAddress>();
                    foreach (string l_oGroup in p_sGroupDestinataire.Split(new[] { ';' }))
                    {
                        foreach (string l_oEmailUser in UserRepository.GetAllEmailUserByGroup(l_oGroup))
                        {
                            if (!m_oMailAddressList.Contains(new MailAddress(l_oEmailUser, "")))
                            {
                                m_oMailAddressList.Add(new MailAddress(l_oEmailUser, ""));
                            }
                        }
                    }
                    Smtp.SendMail(SettingsManager.cStoreSettings.Parameter_Smtp, "[RIO - Flux" + ((p_iTypeFlow != 0) ? " Courrier " : "") + "] " + p_sObjet, p_sMemo + "  <br> Vous avez reçu un nouveau document de " + (l_oUser.FirstName == null ? string.Empty : l_oUser.FirstName) + " " + (l_oUser.LastName == null ? string.Empty : l_oUser.LastName) + " <br> Veuillez suivre le lien suivant  <a href=\"" + ConfigurationManager.AppSettings.Get("Rio.Applications.Web.Url") + "\">" + ConfigurationManager.AppSettings.Get("Rio.Applications.Web.Url") + "</a> et sélectionner l'espace FLUX pour le consulter.", new MailAddress(l_oUser.Email, ""), m_oMailAddressList);
                }

                l_oRet.Add(l_oReturnLong);
            }
            catch (Exception ex)
            {
                l_oReturnLong = new ReturnLong("(p_lEmetteurId<" + p_lEmetteurId + "> p_lChronoId<" + p_lChronoId + "> p_lDocId<" + p_oDocIdList.ToString() + "> p_sObjet<" + p_sObjet + "> p_sMemo<" + p_sMemo + "> p_sGroupDestinataire<" + p_sGroupDestinataire.ToString(";") + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnLong.FullErrorMessage);
                l_oRet.Add(l_oReturnLong);
            }

            return l_oRet;
        }

        /// <summary>
        /// Retreive historical message flow by an user
        /// </summary>
        /// <param name="p_lUserId">User ID</param>
        /// <param name="p_lDocumentId">Document ID</param>
        /// <param name="p_lMsgFlowId">MessageFlow ID</param>
        /// <returns>Historical</returns>
        [Query]
        public IEnumerable<StateMessageFlow> GetHistoryByUserMessageFlow(long p_lUserId, long p_lDocumentId, long p_lMsgFlowId)
        {
            List<StateMessageFlow> l_oRet = new List<StateMessageFlow>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lUserId<" + p_lUserId + "> p_lDocumentId<" + p_lDocumentId + "> p_lMsgFlowId<" + p_lMsgFlowId + ">)...");
                l_oRet = MessageFlowRepository.GetHistoryByUserMessageFlow(p_lUserId, p_lDocumentId, p_lMsgFlowId);
            }
            catch (Exception ex)
            {
                StateMessageFlow l_oReturnValue = new StateMessageFlow("(p_lUserId<" + p_lUserId + "> p_lDocumentId<" + p_lDocumentId + "> p_lMsgFlowId<" + p_lMsgFlowId + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnValue.FullErrorMessage);
                l_oRet.Add(l_oReturnValue);
            }
            return l_oRet;
        }

        /// <summary>
        /// Retreive historical message flow by a group
        /// </summary>
        /// <param name="p_lGroupId">Group ID</param>
        /// <param name="p_lDocumentId">Document ID</param>
        /// <param name="p_lMsgFlowId">MessageFlow ID</param>
        /// <returns>Historical</returns>
        [Query]
        public IEnumerable<StateMessageFlow> GetHistoryByGroupMessageFlow(long p_lGroupId, long p_lDocumentId, long p_lMsgFlowId)
        {
            List<StateMessageFlow> l_oRet = new List<StateMessageFlow>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lGroupId<" + p_lGroupId + "> p_lDocumentId<" + p_lDocumentId + "> p_lMsgFlowId<" + p_lMsgFlowId + ">)...");
                l_oRet = MessageFlowRepository.GetHistoryByGroupMessageFlow(p_lGroupId, p_lDocumentId, p_lMsgFlowId);
            }
            catch (Exception ex)
            {
                StateMessageFlow l_oReturnValue = new StateMessageFlow("(p_lGroupId<" + p_lGroupId + "> p_lDocumentId<" + p_lDocumentId + "> p_lMsgFlowId<" + p_lMsgFlowId + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnValue.FullErrorMessage);
                l_oRet.Add(l_oReturnValue);
            }
            return l_oRet;
        }

        /// <summary>
        /// Retreive historical message flow for all user's groups
        /// </summary>
        /// <param name="p_lUserId">User ID</param>
        /// <param name="p_lDocumentId">Document ID</param>
        /// <param name="p_lMsgFlowId">MessageFlow ID</param>
        /// <returns>Historical</returns>
        [Query]
        public IEnumerable<StateMessageFlow> GetHistoryByUserGroupsMessageFlow(long p_lUserId, long p_lDocumentId, bool p_bIsAdmin)
        {
            List<StateMessageFlow> l_oRet = new List<StateMessageFlow>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lUserId<" + p_lUserId + "> p_lDocumentId<" + p_lDocumentId + ">)...");
                l_oRet = MessageFlowRepository.GetHistoryByUserGroupsMessageFlow(p_lUserId, p_lDocumentId, p_bIsAdmin);
            }
            catch (Exception ex)
            {
                StateMessageFlow l_oReturnValue = new StateMessageFlow("(p_lUserId<" + p_lUserId + "> p_lDocumentId<" + p_lDocumentId + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnValue.FullErrorMessage);
                l_oRet.Add(l_oReturnValue);
            }
            return l_oRet;
        }

        /// <summary>
        /// Add a reminder on a message Flow
        /// </summary>
        /// <param name="p_lUserId">Identifier of the source user</param>
        /// <param name="p_lMessageFowId">Identifier of the Message flow</param>
        /// <param name="p_sGrpId">Group Identifier</param>
        /// <returns>A ReturnBoolean object with all necessary informations</returns>
        [Query(HasSideEffects = true)]
        public IEnumerable<ReturnBoolean> AddMessageFlowReminder(long p_lMessageFowId, List<long> p_oGrpIdList)
        {
            List<ReturnBoolean> l_oRet = new List<ReturnBoolean>();
            User l_oCurrentUser = GetUser();
            try
            {
                foreach (long l_lGroupId in p_oGrpIdList)
                {
                    NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lMessageFowId<" + p_lMessageFowId + "> p_sGrpId<" + p_oGrpIdList.ToString() + ">)...");
                    l_oRet = MessageFlowRepository.AddMessageFlowReminder(l_oCurrentUser.UserId, p_lMessageFowId, l_lGroupId);
                }
            }
            catch (Exception ex)
            {
                ReturnBoolean l_oReturnValue = new ReturnBoolean("(p_lMessageFowId<" + p_lMessageFowId + "> p_sGrpId<" + p_oGrpIdList.ToString() + ">)", new Exception(ex.Message));
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnValue.FullErrorMessage);
                l_oRet.Add(l_oReturnValue);
            }
            return l_oRet;
        }

        /// <summary>
        /// Retreive all doc state
        /// </summary>
        /// <returns>All doc state</returns>
        [Query]
        public IEnumerable<StateDoc> GetStateDocList()
        {
            List<StateDoc> l_oRet = new List<StateDoc>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("...");
                l_oRet = MessageFlowRepository.GetStateDocList(); // FlowManager.StateDocs;
            }
            catch (Exception ex)
            {
                StateDoc l_oReturnValue = new StateDoc("", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnValue.FullErrorMessage); 
                l_oRet.Add(l_oReturnValue);
            }
            return l_oRet;
        }
    
        /// <summary>
        /// Update the doc status
        /// </summary>
        /// <param name="p_lDocumentIdList">Document ID</param>
        /// <param name="p_lDocEtatId">StateDoc ID</param>
        /// <param name="p_lMsgFlowId">MsgFlow ID</param>
        /// <param name="p_lGroupListId">Group ID</param>
        [Query(HasSideEffects = true)]
        public IEnumerable<ReturnObject> UpdateStateDocList(List<long> p_lDocumentIdList, long p_lDocEtatId, long p_lMsgFlowId, List<long> p_lGroupListId, string p_sDocEtatGroupComment, bool p_bSendAlertByEmail)
        {
            List<ReturnObject> l_oDocuments = new List<ReturnObject>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("p_lDocumentIdList<" + p_lDocumentIdList.ToString() + "> p_lDocEtatId<" + p_lDocEtatId + "> p_lMsgFlowId<" + p_lMsgFlowId + "> p_lGroupId<" + p_lGroupListId.ToString() + "> p_sDocEtatGroupComment<" + p_sDocEtatGroupComment + ">)...");
                User l_oUser = GetUser();

                
                Tuple<bool, string> l_tUpdateStateDocList = null;
                foreach (long l_oDocId in p_lDocumentIdList)
                {
                    l_tUpdateStateDocList = DA_MessageFlow.UpdateStateDocList(l_oDocId, p_lDocEtatId, p_lMsgFlowId, l_oUser.UserId, p_lGroupListId, p_sDocEtatGroupComment);
                }

                if (l_tUpdateStateDocList!=null && l_tUpdateStateDocList.Item1 == true)
                {
                    List<MailAddress> l_oMailAdresses = new List<MailAddress>();
                    l_oMailAdresses.Add(new MailAddress(l_tUpdateStateDocList.Item2, string.Empty));

                    MessageFlow l_lstMessageFlow = Rio.Flow.Business.Repository.Translator.MessageFlowTranslator.TranslateMessageFlow(DA_MessageFlow.GetMessageFlowById(p_lMsgFlowId));

                    if (p_bSendAlertByEmail)
                        Smtp.SendMail(SettingsManager.cStoreSettings.Parameter_Smtp, "[RIO - Flux Action]  " + l_lstMessageFlow.Objet, string.Format("<br>L'utilisateur {0} a traité vos documents dans le flux. Pour suivre les traitements cliquez sur ce lien : ", (l_oUser.FirstName == null ? string.Empty : l_oUser.FirstName) + " " + (l_oUser.LastName == null ? string.Empty : l_oUser.LastName)) + "  <a href=\"" + ConfigurationManager.AppSettings.Get("Rio.Applications.Web.Url") + "\">" + ConfigurationManager.AppSettings.Get("Rio.Applications.Web.Url") + "</a>", new MailAddress(l_oUser.Email, ""), l_oMailAdresses);
                }
                // All is OK
                l_oDocuments.Add(new ReturnObject());
            }
            catch (Exception ex)
            {
                ReturnObject l_oDocument = new ReturnObject("(p_lDocumentIdList<" + p_lDocumentIdList.ToString() + "> p_lDocEtatId<" + p_lDocEtatId + "> p_lMsgFlowId<" + p_lMsgFlowId + "> p_lGroupId<" + p_lGroupListId.ToString() + "> p_sDocEtatGroupComment<" + p_sDocEtatGroupComment + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oDocument.FullErrorMessage);
                l_oDocuments.Add(l_oDocument);
            }
            return l_oDocuments;
        }

        /// <summary>
        /// Delete a list msg flow
        /// </summary>
        /// <param name="p_oMsgFlowIds">List of identifier of message flow</param>
        [Query(HasSideEffects = true)]
        public IEnumerable<ReturnObject> DeleteMessageFlow(List<long> p_oMsgFlowIds)
        {
            List<ReturnObject> l_oReturnObjects = new List<ReturnObject>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_oMsgFlowIds<" + p_oMsgFlowIds + ">)...");
                ReturnObject l_oReturnObject = MessageFlowRepository.DeleteMessageFlow(p_oMsgFlowIds);

                // Inform parent to complete process
                l_oReturnObjects.Add(l_oReturnObject);
            }
            catch (Exception ex)
            {
                ReturnObject l_oReturnObject = new ReturnObject("(p_oMsgFlowIds<" + p_oMsgFlowIds + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnObject.FullErrorMessage);
                l_oReturnObjects.Add(l_oReturnObject);
            }
            return l_oReturnObjects;
        }

        /// <summary>
        /// Delete list msg flow from user
        /// </summary>
        /// <param name="p_oMsgFlowId">List of msg flow ID</param>
        /// <param name="p_oRguId">List of Rguid</param>
        [Query(HasSideEffects = true)]
        public IEnumerable<ReturnObject> DeleteMessageFlowUser(List<long> p_oMsgFlowId, List<long> p_oRguId)
        {
            List<ReturnObject> l_oReturnObjects = new List<ReturnObject>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_oMsgFlowId<" + p_oMsgFlowId + "> p_oRguId<" + p_oRguId + ">)...");
                ReturnObject l_oReturnObject = MessageFlowRepository.DeleteMessageFlowUser(p_oMsgFlowId, p_oRguId);

                // Inform parent to complete process
                l_oReturnObjects.Add(l_oReturnObject);
            }
            catch (Exception ex)
            {
                ReturnObject l_oReturnObject = new ReturnObject("(p_oMsgFlowId<" + p_oMsgFlowId + "> p_oRguId<" + p_oRguId + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnObject.FullErrorMessage);
                l_oReturnObjects.Add(l_oReturnObject);
            }
            return l_oReturnObjects;
        }

        /// <summary>
        /// Retreive a valid chrono
        /// </summary>
        /// <param name="p_lChronoTypeId">Chrono type identifier</param>
        /// <param name="p_iCreationVolee">0(needed to create) otherwise 1</param>
        /// <returns></returns>
        [Query]
        public IEnumerable<Item> GenerateChrono(long FolderTypeId, string InputedTypeFlowValue)
        {
            List<Item> l_oReturnStrings = new List<Item>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(FolderTypeId<" + FolderTypeId + "> InputedTypeFlowValue<" + InputedTypeFlowValue + ">)...");
                using (RioWCFWorkflow.WCFWorkflowClient client =  WorkflowController.GetRioWCFWorkflowClient())
                {
                    var it = client.RequestChrono(FolderTypeId, GetUser().UserId, InputedTypeFlowValue);
                    l_oReturnStrings.Add(it);
                }
            }
            catch (Exception ex)
            {
                string l_oReturnString = "(FolderTypeId<" + FolderTypeId + "> InputedTypeFlowValue<" + InputedTypeFlowValue + ">)\r" + ex.StackTrace;
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnString);
                l_oReturnStrings.Add(new Item(l_oReturnString, ex));
            }
            return l_oReturnStrings;
        }

        /// <summary>
        /// Delete list msg flow from user
        /// </summary>
        /// <param name="p_oMsgFlowId">List of msg flow ID</param>
        /// <param name="p_oRguId">List of Rguid</param>
        [Query]
        public IEnumerable<ReturnObject> UpdateMessageFlowLastReadDate(long p_lMessageFlowId)
        {
            List<ReturnObject> l_oReturnObjects = new List<ReturnObject>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lMessageFlowId<" + p_lMessageFlowId + ">)...");
                ReturnObject l_oReturnObject = MessageFlowRepository.UpdateMessageFlowLastReadDate(p_lMessageFlowId);

                // Inform parent to complete process
                l_oReturnObjects.Add(l_oReturnObject);
            }
            catch (Exception ex)
            {
                ReturnObject l_oReturnObject = new ReturnObject("(p_lMessageFlowId<" + p_lMessageFlowId + "> )", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnObject.FullErrorMessage);
                l_oReturnObjects.Add(l_oReturnObject);
            }
            return l_oReturnObjects;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_sMailSubject"> Mail subject</param>
        /// <param name="p_sMailMemo">Mail memo</param>
        /// <param name="p_sGroupList">Mailto group list</param>
        /// <returns>ReturnObject</returns>
        [Query(HasSideEffects = true)]
        public IEnumerable<ReturnObject> Remind(string p_sMailSubject, string p_sMailMemo, string p_sGroupList)
        {
            List<ReturnObject> l_oReturnObjects = new List<ReturnObject>();
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_sMailSubject<" + p_sMailSubject + "> p_sMailMemo<" + p_sMailMemo + "> p_sGroupList<" + p_sGroupList + ">)...");
                User l_oUser = GetUser();
                
                List<MailAddress> m_oMailAddressList = new List<MailAddress>();
                foreach (string l_oGroup in p_sGroupList.Split(new[] { ';' }))
                {
                    if (!String.Empty.Equals(l_oGroup))
                    {
                        List<string> l_oGroupUserEmail = UserRepository.GetAllEmailUserByGroup(l_oGroup);
                        foreach (string l_oEmailUser in l_oGroupUserEmail)
                        {
                            m_oMailAddressList.Add(new MailAddress(l_oEmailUser, ""));
                        }
                    }
                }
                ReturnBoolean l_oRet = Smtp.SendMail(SettingsManager.cStoreSettings.Parameter_Smtp, "[RIO - Flux Relance] " + p_sMailSubject, "L'utilisateur vous rappelle que vous avez des documents à traiter. Veuillez suivre le lien suivant <a href=\"" + ConfigurationManager.AppSettings.Get("Rio.Applications.Web.Url") + "\">" + ConfigurationManager.AppSettings.Get("Rio.Applications.Web.Url") + "</a> et sélectionner le module FLUX. <br><br><br> Message : " + p_sMailMemo, new MailAddress(l_oUser.Email, ""), m_oMailAddressList);
               l_oReturnObjects.Add(l_oRet);
            }
            catch (Exception ex)
            {
                ReturnObject l_oReturnObject = new ReturnObject("(p_sMailSubject<" + p_sMailSubject + "> )", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnObject.FullErrorMessage);
                l_oReturnObjects.Add(l_oReturnObject);
            }

            return l_oReturnObjects;
        }

        /// <summary>
        /// Releases a chrono  chrono.
        /// </summary>
        /// <param name="p_sChronoName">Name of the P_S chrono.</param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public ReturnBoolean ReleaseChrono(string p_sChronoName)
        {
            ReturnBoolean l_oRet = null;
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_sChronoName<" + p_sChronoName + ">)...");
                l_oRet = MessageFlowRepository.ReleaseChrono(p_sChronoName);
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnBoolean("(p_sChronoName<" + p_sChronoName + "> )", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oRet.FullErrorMessage);

            }
            return l_oRet;
        }

        /// <summary>
        /// Reserves a chrono number.
        /// </summary>
        /// <param name="p_sChronoName">Name of the P_S chrono.</param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public ReturnBoolean FixChrono(string p_sChronoName)
        {
            ReturnBoolean l_oRet = null;
            try
            {
                //TODO: WKF call WCF Service to fix chrono if a mailing workflow is attached to 


                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_sChronoName<" + p_sChronoName + ">)...");
                l_oRet = MessageFlowRepository.FixChrono(p_sChronoName);
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnBoolean("(p_sChronoName<" + p_sChronoName + "> )", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oRet.FullErrorMessage);

            }
            return l_oRet;
        }

        [Invoke(HasSideEffects = true)]
        public ReturnObject AddSendersToMessageFlow(List<long> p_oSenders, long p_lMessageFlowId)
        {
            ReturnObject l_oRet = null;
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lMessageFlowId<" + p_lMessageFlowId + ">)...");
                l_oRet = MessageFlowRepository.AddSenderToMessageFlow(p_oSenders, p_lMessageFlowId);
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnBoolean("(p_lMessageFlowId<" + p_lMessageFlowId + "> )", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oRet.FullErrorMessage);

            }
            return l_oRet;
        }

    }
}